<template>
  <div class="container">
    <div class="top">
      <div class="header">
        <a href="/">
          <img src="../assets/info.svg" alt="logo" class="logo">
          <span class="title">IvyMovie</span>
        </a>
      </div>
      <div class="desc">
        IvyMovie后端管理系统
      </div>
    </div>
    <div class="main">
<!--   prop="username"设置对应的校验名   -->
      <el-form :model="loginForm"
               class="user-layout-login"
               ref="loginForm" :rules="rules">
        <el-alert :title="loginForm.errorMsg" type="error" v-show="loginForm.errorVisible">
        </el-alert>

        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          </el-input>
        </el-form-item>
        <el-form-item prop="password" >
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码">
          </el-input>
        </el-form-item>

        <el-form-item style="margin-top: 24px;">
          <el-button :loading="isLoading" class="login-button" @click="submitForm('loginForm')" size="medium" type="primary" style="width:100%;">
            <span>登 录</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="footer">
      <div class="links">
        <a href="_self">帮助</a>
        <a href="_self">隐私</a>
        <a href="_self">条款</a>
      </div>
      <div class="copyright">
        Ivy Movie
      </div>
    </div>
  </div>


</template>

<script>
import {login} from "@/api/system";
import {setJwtToken} from "@/lib/utils/TokenUtil";

export default {
  name:'Login',
  data(){
    return {
      isLoading:false,
      loginForm:{
        username:"",
        password:"",
        errorMsg:"",
        errorVisible: false
      },
      rules:{
        username:[
          { required: true, message: '请输入帐号', trigger: 'blur' },
          { min:2,message: '请输入正确的帐号', trigger:['blur','change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    submitForm(formName) {
      this.isLoading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login({username:this.loginForm.username,password:this.loginForm.password})
          .then(res =>{
              setJwtToken(res.data)
              console.log(res)
              this.$router.push('/home')
          }).catch(err =>{
            this.loginForm.errorMsg = err.message;
            this.loginForm.errorVisible = true
          }).finally(()=>{
            this.isLoading = false
          })
        } else {
          this.isLoading = false
          this.$message.error('用户名或者密码错误');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="less">
.container {
  width: 100%;
  height: 100%;
  background: #f0f2f5 url(../assets/login.svg) no-repeat 50%;
  background-size: 100%;
  //padding: 110px 0 144px;
  position: relative;

  .top {
    text-align: center;
    padding-top: 110px;
    .header {
      height: 44px;
      line-height: 44px;

      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
        border-style: none;
      }

      .title {
        font-size: 33px;
        color: rgba(0, 0, 0, .85);
        font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }

    }

    .desc {
      font-size: 14px;
      color: rgba(0, 0, 0, .45);
      margin-top: 12px;
      margin-bottom: 40px;
    }

  }

  .main {
    min-width: 260px;
    width: 368px;
    margin: 0 auto;

    .user-layout-login {
      label {
        font-size: 14px;
      }

      .getCaptcha {
        display: block;
        width: 100%;
        height: 40px;
      }

      .forge-password {
        font-size: 14px;
      }

      button.login-button {
        padding: 0 15px;
        font-size: 16px;
        height: 40px;
        width: 100%;
      }

      .user-login-other {
        text-align: left;
        margin-top: 24px;
        line-height: 22px;

        .item-icon {
          font-size: 24px;
          color: rgba(0, 0, 0, 0.2);
          margin-left: 16px;
          vertical-align: middle;
          cursor: pointer;
          transition: color 0.3s;

          &:hover {
            color: #1890ff;
          }
        }

        .register {
          float: right;
        }
      }
    }

  }

  .footer {
    position: absolute;
    width: 100%;
    bottom: 0;
    margin: 48px 0 24px;
    text-align: center;

    .links {
      margin-bottom: 8px;
      font-size: 14px;
    }

    .copyright {
      color: rgba(0, 0, 0, .45);
      font-size: 14px;
    }

  }
}
</style>
